from django.shortcuts import render

# Create your views here.
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from apps.recruit.models import Recruit,Enterprise,City
from apps.recruit.serializer import DetailSerializer,RecruitSerializerSimple,\
    EnterpriseSerializerSimple,EnterpriseSerializer,HotCitySerializer,RecommendJobSerializer
from rest_framework.response import Response


#推荐职位
class RecommendJobView(ModelViewSet):
    #按照查询时间查询四条数据
    queryset = Recruit.objects.filter(state=1).order_by('-createtime')[0:4]
    serializer_class = RecruitSerializerSimple

    def get_commend_job(self,request):
        #获取四条数据
        data = self.get_queryset()
        serializer = self.serializer_class(instance=data,many=True)

        return Response(serializer.data)


#最新职位
class NewestJobView(ModelViewSet):
    queryset = Recruit.objects.filter(state=1).order_by('-createtime')[0:4]
    serializer_class = RecruitSerializerSimple

    def get_commend_job(self,request):
        #获取四条数据
        data = self.get_queryset()
        serializer = self.serializer_class(instance=data,many=True)

        return Response(serializer.data)


#热门企业
class HotEnterpriseView(ModelViewSet):
    queryset = Enterprise.objects.all()
    serializer_class = EnterpriseSerializerSimple

    def get_hot_enterprise(self,request):
        data = self.get_queryset().order_by('-visits')[0:5]
        serializer = self.serializer_class(instance=data,many=True)

        return Response(serializer.data)


#职位详情
class DetailJobView(APIView):
    def get(self,request,id):
        data = Recruit.objects.filter(id=id)
        serializer = DetailSerializer(instance=data,many=True)
        for i in serializer.data:
            return Response(i)


#更新招聘信息访问量
class RecruitVisitCountView(APIView):
    def put(self,request,id):
        recruit = Recruit.objects.get(id=id)
        recruit.visits += 1
        recruit.save()
        return Response({'success': True, 'message': '更新成功'})


#收藏招聘信息
class CollectionRecruitView(APIView):
    def post(self,request,id):
        try:
            user = request.user
        except Exception:
            user = None
        if user is not None and user.is_authenticated():
            recruit = Recruit.objects.get(id=id)
            data = recruit.users.add(user)
            DetailSerializer(data=data)
            recruit.save()
            return Response({'success': True, 'message': '收藏成功'})
        else:
            return Response({'success': False, 'message': '未登录'},status=400)


#取消收藏
class RemoveCollectionView(APIView):
    def post(self,request,id):
        try:
            user = request.user
        except Exception:
            user = None
        if user is not None and user.is_authenticated():
            recruit = Recruit.objects.get(id=id)
            data = recruit.users.remove(user)
            DetailSerializer(data=data)
            recruit.save()
            return Response({'success': True, 'message': '取消收藏成功'})
        else:
            return Response({'success': False, 'message': '未登录'},status=400)

#企业详情
class DetailEnterpriseView(APIView):
    def get(self,request,id):
        data = Enterprise.objects.filter(id=id)
        serializer = EnterpriseSerializer(instance=data,many=True)
        for i in serializer.data:
            return Response(i)


#更新企业访问量
class EnterpriseVisitCountView(APIView):
    def put(self,request,id):
        enterprise = Enterprise.objects.get(id=id)
        enterprise.visits += 1
        enterprise.save()
        return Response({'success': True, 'message': '更新成功'})


#收藏企业信息
class CollectionEnterpriseView(APIView):
    def post(self,request,id):
        try:
            user = request.user
        except Exception:
            user = None
        if user is not None and user.is_authenticated():
            enterpries = Enterprise.objects.get(id=id)
            data = enterpries.users.add(user)
            EnterpriseSerializer(data=data)
            enterpries.save()
            return Response({'success': True, 'message': '收藏成功'})
        else:
            return Response({'success': False, 'message': '未登录'}, status=400)


#收藏企业信息
class RemoveCollectionEnterpriseView(APIView):
    def post(self,request,id):
        try:
            user = request.user
        except Exception:
            user = None
        if user is not None and user.is_authenticated():
            enterpries = Enterprise.objects.get(id=id)
            data = enterpries.users.remove(user)
            EnterpriseSerializer(data=data)
            enterpries.save()
            return Response({'success': True, 'message': '取消收藏成功'})
        else:
            return Response({'success': False, 'message': '未登录'}, status=400)


#热门城市
class HotCityView(ModelViewSet):
    queryset = City.objects.all()
    serializer_class = HotCitySerializer

    def get_hot_city(self,request):
        data = self.get_queryset()
        serializer = self.serializer_class(instance=data,many=True)

        return Response(serializer.data)


#搜索,根据职位关键字和地址查询
class SearchView(APIView):
    def post(self,request):
        job_list = []
        cityname = request.data.get('cityname')
        keyword = request.data.get('keyword')
        if cityname and not keyword:
            recruit = Recruit.objects.filter(city=cityname).order_by('-createtime')
            serializer = RecommendJobSerializer(instance=recruit, many=True)
            return Response(serializer.data)
        elif keyword and not cityname:
            recruit = Recruit.objects.all()
            for job in recruit:
                if job.jobname.find(keyword) == -1:
                    job_list.append(job)
            serializer = RecommendJobSerializer(instance=job_list, many=True)
            return Response(serializer.data)
        elif cityname and keyword:
            recruit = Recruit.objects.filter(city=cityname).order_by('-createtime')
            for job in recruit:
                if job.jobname.lower().find(keyword.lower()) != 1:
                    job_list.append(job)
            serializer = RecommendJobSerializer(instance=job_list, many=True)
            return Response(serializer.data)
        else:
            recruit = Recruit.objects.filter(city='北京').order_by('-createtime')
            serializer = RecommendJobSerializer(instance=recruit, many=True)
            return Response(serializer.data)
